<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="intro-wrap" style="">
        <ul>
          <li>短信签名建议为应用名。若签名/模板内容侵犯到第三方权益须获得第三方的真实授权。</li>
          <li>单个签名长度介于2到15个字符之间，只支持数字、英文、中文、°。</li>
          <li>无须添加【】、()、[]符号，短信发送会自带【】、()、[]符号，避免重复。</li>
          <li>如申请的短信签名和公司名称看不出关联，请同时上传公司营业执照、公司与申请签名的关系证明，证明可通过『其他证明信息』上传。</li>
          <li>验证码签名和通知类签名一般在一个工作日内审核完成，运营类签名一般在三个工作日内审核完成。</li>
          <li>三证合一的企业需要上传带红字公章的三证合一营业执照，非三证合一的企业，需在其他证明材料中上传带公章的授权税务登记证、授权组织机构代码。</li>
          <!-- <li>短信/模板模板申请相见规范。</li> -->
        </ul>
      </div>
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            
            <a-col :md="8" :sm="24">
              <a-form-item label="产品">
                <AWM-select
                  v-model="queryParam.productId"
                  placeholder="请选择产品"
                  :addition="computedQueryParam"
                  funStr="productByNoAuth"
                ></AWM-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="状态">
                <a-select v-model="queryParam.status" placeholder="请选择状态">
                  <a-select-option v-for="it in statusList" :key="it.id" :value="it.id">{{ it.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="类型">
                <a-select v-model="queryParam.type" placeholder="请选择类型">
                  <a-select-option v-for="it in templateTypeList" :key="it.id" :value="it.id">{{ it.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <!-- <a-col :md="8" :sm="24">
              <a-form-item label="时间">
                <a-range-picker
                  show-time
                  v-model="queryParam.buildTime"
                  style="width: 100%"
                  :placeholder="['请选择开始日期', '请选择结束日期']"
                />
              </a-form-item>
            </a-col> -->
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => ((this.queryParam = {}), (this.need = {}))"
                  >重置</a-button
                >
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="handleAdd">新建</a-button>
      </div>
      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        showPagination="auto"
        :scroll="{ x: 'auto' }"
      > 
      
        <span slot="imageUrl" slot-scope="text, record">
          <div class="img-wrap">
            <div class="img-wrap-item" v-if="record.licenseFile"  @click="handleImgShow(record)">
              
              <img :src="`${record.licenseFile}`"  alt="">
              <!-- <div>{{record.signType == 3?'个人身份证正面':'营业执照'}}</div> -->
            </div>
            <div class="img-wrap-item" v-if="record.mandateFile"  @click="handleImgShow(record)">
              
              <img :src="`${record.mandateFile}`" alt="">
              <!-- <div>授权文件</div> -->
            </div>
            <div class="img-wrap-item" v-if="record.mandateLicenseFile"  @click="handleImgShow(record)">
              
              <img :src="`${record.mandateLicenseFile}`" alt="">
              <!-- <div>{{record.signType == 3?'个人身份证反面':'授权单位营业执照'}}</div> -->
            </div>
            
          </div>
        </span>
        <span slot="status" slot-scope="text">
          <a-tag :color="text | statusFilter(statusList, 'status')">{{
            text | statusFilter(statusList, 'name')
          }}</a-tag>
        </span>
        <span slot="type" slot-scope="text">
          <a-tag :color="text | statusFilter(templateTypeList, 'status')">{{
            text | statusFilter(templateTypeList, 'name')
          }}</a-tag>
        </span>
        <span slot="signSource" slot-scope="text">
          <a-tag :color="text | statusFilter(signSourceList, 'status')">{{
            text | statusFilter(signSourceList, 'name')
          }}</a-tag>
        </span>
        
        
        <span slot="signType" slot-scope="text">
          <a-badge style="font-size:10px;"  :color="text | statusFilter(signTypeList, 'status')" :text="text | statusFilter(signTypeList, 'sort')" />
        </span>
        <span slot="amount" slot-scope="text">￥{{ text | NumberFormat }}</span>
        <span slot="action" slot-scope="text, record">

          <template >
            <span  v-if="record.status == 3">
              <a @click="handleEdit(record)">编辑</a>
              <a-divider type="vertical" />
            </span>
            <span>
              <a @click="handleDel(record)">删除</a>
            </span>
          </template>
        </span>
        <span slot="remark" slot-scope="text">
          <template>
            <div class="remark-wrap">{{ text }}</div>
          </template>
        </span>
      </s-table>
    </a-card>
    <img-show :visible="img.visible" :list="img.list" @cancel="handleImgCancel"></img-show>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import { STable } from '@/components'
import { signatureAdd,
signatureList,
signatureUpdate } from '@/api/sms'
import { msgStatusList, signatureStatusList, signTypeList, templateTypeList, signSourceList } from '@/utils/other'
// import StepByStepModal from './modules/StepByStepModal'
import { mapGetters } from 'vuex'
// import SignatureForm from './modules/SignatureForm.vue'
import AWMSelect from '@/components/AWMSelect'
import ImgShow from '../mouduls/ImgShow'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    key: 'id',
    width: 100,
  },
  {
    title: '签名',
    dataIndex: 'name',
    key: 'name',
    width: '150px',
    customRender: (text) => text || '--',
  },
  {
    title: '签名来源',
    dataIndex: 'signSource',
    width: 180,
    scopedSlots: { customRender: 'signSource' },
  },
  {
    title: '签名用途',
    width: '200px',
    dataIndex: 'signType',
    scopedSlots: { customRender: 'signType' },
  },
  
  {
    title: '图片附件',
    dataIndex: 'imageUrl',
    width: '300px',
    scopedSlots: { customRender: 'imageUrl' },
  },
  
  // {
  //   title: '定时发送',
  //   dataIndex: 'timing',
  //   width: '200px',
  //   customRender: (text) => text || '--',
  // },
  {
    title: '状态',
    dataIndex: 'status',
    width: '100px',
    scopedSlots: { customRender: 'status' },
  },
  
  {
    title: '驳回理由',
    dataIndex: 'refuseReason',
    width: '200px',
  },
  {
    title: '添加时间',
    dataIndex: 'addTime',
    width: '220px',
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '120px',
    scopedSlots: { customRender: 'action' },
  },
]

export default {
  name: 'log-list',
  components: {
    STable,
    AWMSelect,
    ImgShow
  },
  data() {
    this.columns = columns
    return {
      open: false,
      need: {},
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 查询参数
      parameter: {
        pageNum: 1,
        pageSize: 10,
      },
      queryParam: {
      },
      data: [],
      img: {
        visible: false, 
        list: []
      },
      // 状态
      statusList: signatureStatusList,
      signSourceList,
      templateTypeList,
      signTypeList,
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam, this.computedQueryParam)
        // requestParameters.addUser = this.$store.getters.userInfo.id || 0
        return signatureList(requestParameters).then((res) => {
          let result = {
            data: res.data.data,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalCount: res.data.total,
            totalPage: res.data.pages,
          }
          return result
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
    }
  },
  created() {},
  computed: {
    ...mapGetters({ baseUrl: 'baseUrl', userInfo: 'userInfo' }),
    isTiming: function () {
      console.log(this.$route.path.indexOf('/timing'))
      return this.$route.path.indexOf('/timing') != -1
    },
    computedQueryParam: function () {
      return {
        isInland: this.isInternational,
      }
    },
  },
  methods: {
    handleImgShow(record){
      this.img.visible = true
      let list = [record.licenseFile, record.mandateFile, record.mandateLicenseFile]
      list = list.filter(v=>!!v)
      this.img.list = list
      // handleImgShow
    },
    handleImgCancel(){
      this.img.visible = false
      this.img.list = []
    },
    handleCheckList(record) {
      this.check.mdl = { ...record }
      this.check.visible = true
    },
    handleAdd() {
      this.$router.push({ path: `/signature/add`, query: { pageNum: this.parameter.pageNum } })
    },

    handleEdit(record) {
      this.$store.dispatch('SignatureToUpdate', record).then((res) => {
        this.$router.push({ path: `/signature/update/${record.id}`, query: { pageNum: this.parameter.pageNum } })
      })
    },
    
    saveCB() {
      const form = this.$refs.createModal.form
      this.visible = false
      this.confirmLoading = false
      // 重置表单数据
      form.resetFields()
      // 刷新表格
      this.$refs.table.refresh()
    },
    handleOk() {
      let self = this;
      const modal = this.$refs.createModal
      const form = modal.form
      this.confirmLoading = true
      console.log(111);
      form.validateFields((errors, values) => {
        console.log(errors, values);
        if (!errors) {
          values.licenseFile = modal.licenseFile.file
            values.mandateLicenseFile = modal.mandateLicenseFile.file
            values.mandateFile = modal.mandateFile.file
          if (values.id > 0) {
            
            signatureUpdate(values)
              .then((res) => {
                this.saveCB()
                this.$message.success('修改成功!')
              })
              .catch((err) => {
                this.saveCB()
              })
          } else {
            

            console.log(222);
            delete values.id
            signatureAdd(values)
              .then((res) => {
                this.saveCB()
                this.$message.success('新增成功!')
              })
              .catch((err) => {
                this.saveCB()
              })
          }
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.visible = false
      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    handleDel(a_record) {
      let self = this
      const modal = self.$confirm({
        title: '用户删除',
        content: `是否删除用户:${a_record.name}`,
        okText: '确认',
        cancelText: '取消',
        onOk: () => self.handleDelete(a_record.id, modal, -1),
        onCancel: () => {},
      })
    },
    handleDelete(a_id, a_modal, a_type) {
      return new Promise((resolve, reject) => {
        let data = { id: a_id, status: a_type }
        signatureUpdate(data)
          .then((res) => {
            this.$refs.table.refresh()
            this.$message.success('删除成功!')
            resolve()
          })
          .catch((err) => {
            a_modal.destroy()
            this.$message.error('操作失败!')
          })
      })
    },
  },
}
</script>
<style scoped lang="less">
.param-wrap {
  min-width: 200px;
  max-width: 300px;
}

.a-tag-wrap {
  font-size: 12px;
  /deep/ .ant-tag {
    line-height: 18px;
    padding: 0 4px;
  }
  div {
    margin-bottom: 2px;
    &:last-child {
      margin-bottom: 0;
    }
  }
}
.img-wrap{
  display: flex;
  .img-wrap-item{
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    width: 70px;
    padding: 5px;
    margin-right:5px;
    // background-color: #ccc;
    img{
      max-width: 50px;
      max-height: 50px;
      margin: 0 auto;
    }
    div{
      font-size:10px;
      text-align: center;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
}
.intro-wrap {
  background: #f0f2f5;
  padding: 24px;
  margin-bottom: 24px;
  ul {
    padding-left: 5px;
    list-style: disc;
    li {
    }
  }
}
</style>

 